# @Time : 2021/4/13 14:53
# @Author : Fioman 
# @Phone : 13149920693
"""
和前端通信的工具接口,这个模块主要是用来发送前端消息的.
"""
import json
from datetime import datetime

from db_tools.db_handler import DbHandler
from eap_mysql import mysql_access
from tools.websocket_queue import WebSocketQueue

msgQueue = WebSocketQueue().websocketQueue
db = DbHandler()


def send_cutter_camera_cal_state(msg):
    """
    更新铡刀标定的时候的使用到的变量信息
    :param msg: 消息内容
    :return:
    """
    msgType = "cutterCamCalState"
    send_msg_to_ui(msgType, msg)


def send_prod_board_data_to_ui(new_value):
    """
    发送prodBoardData数据
    """
    msgType = "produceState"
    msg = {
        "val": "prodBoardData",
        "data": new_value
    }
    send_msg_to_ui(msgType, msg)


def send_working_info_to_ui(new_value):
    new_value["table_used"] = (datetime.now() - datetime.strptime(
        new_value["table_start_day"], "%Y-%m-%d")).days
    msg_type = "produceState"
    msg = {
        "val": "workingInfo",
        "data": new_value
    }
    send_msg_to_ui(msg_type, msg)


def send_line_camera_joint_state(msg):
    """
    更新线阵相机的拼接界面的参数信息.
    :param msg: 消息内容
    :return:
    """
    msgType = "lineCamJointState"
    send_msg_to_ui(msgType, msg)


def send_line_camera_cal_state_to_ui(msg):
    """
    更新线阵相机的界面,主要用来刷新这个界面的状态更新
    :param msg: 消息内容
    :return:
    """
    msgType = "lineCamCalState"
    send_msg_to_ui(msgType, msg)


def send_cbj_zmc_tool_state_to_ui(msg):
    """
    更新裁切机工具界面的状态信息
    :param msg 要发送的消息
    :return:
    """
    msgType = "cbjZmcToolState"
    send_msg_to_ui(msgType, msg)


def send_settings_update_to_ui():
    """
    更新设置界面的数据的状态
    :return:
    """
    msgType = 'settingsUpdate'
    msg = {
        "val": "1"
    }
    send_msg_to_ui(msgType, msg)


def send_working_info_state_to_ui():
    """
    更新workinginfo的状态,这里
    :return:
    """
    msgType = "produceState"
    msg = {
        "val": 'workingInfo'
    }
    send_msg_to_ui(msgType, msg)


def send_produce_state_to_ui(msgVal):
    """
    发送生产作业的页面的状态信息
    :param msgVal 消息内容
    :return:
    """
    msgType = "produceState"
    send_msg_to_ui(msgType, msgVal)


def send_eap_msg_state_to_ui(msgVal):
    """
    发送eap页面的状态消息给前端
    :param msgVal 消息内容
    """
    msgType = "eapMsgShow"
    send_msg_to_ui(msgType, msgVal)


def send_order_state_to_ui(msg):
    """
    更新工单状态
    :param msg:使用的key值
    :return:
    """
    msgType = "orderStates"
    send_msg_to_ui(msgType, msg)


def update_eap_params_to_ui():
    """
    更新Eap的参数表
    """
    res, new_data = db.get_eap_params()
    if res != "ok":
        print(new_data)
        new_data = []
    else:
        new_data = [new_data]
    msg_type = "eapStateUpdate"
    msg_val = {
        "val": "updateEapParams",
        "new_value": new_data
    }
    send_msg_to_ui(msg_type, msg_val)


def update_eap_table_data_to_ui(msgType):
    """
    更新pc_data_craft 表数据到UI
    """
    newVal = {}
    if msgType == "updatePcDataCraft":
        newVal = mysql_access.get_pc_data_craft_data()
    elif msgType == "updatePcDataManual":
        newVal = mysql_access.get_pc_data_manual_data()
    elif msgType == "updatePcDataAlarm":
        newVal = mysql_access.get_pc_data_alarm_data()
    elif msgType == "updatePcDataStatus":
        newVal = mysql_access.get_pc_data_status_data()
    elif msgType == "updatePcDataEmp":
        newVal = mysql_access.get_pc_data_emp_data()
    elif msgType == "updatePcDataCount":
        newVal = mysql_access.get_pc_data_count_data()
    elif msgType == "updatePcSignalStatus":
        newVal = mysql_access.get_pc_signal_status_data()
    elif msgType == "updateEapSignalStatus":
        newVal = mysql_access.get_eap_signal_status_data()
    elif msgType == "updateSizeLayoutData":
        newVal = db.get_data_list(db.sizeLayoutTable)[1]
    elif msgType == "sizeLayoutNotFound":
        newVal = "Eap工单下发失败: 此工单尺寸对应的拼板方式和排板方式在本地没有配置"

    msg_type = "eapStateUpdate"
    msg_val = {
        "val": msgType,
        "new_value": newVal
    }
    send_msg_to_ui(msg_type, msg_val)


def send_msg_to_ui(msgType, msg):
    """
    将websocketQueue的内容发送给前端.
    :param msgType: 消息类型
    :param msg: 消息内容
    :return:
    """
    msgQueue.put(json.dumps({msgType: msg}))
